package com.gouwudai.gobbs.dao.impl;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.dao.IRegCounterDAO;
import com.gouwudai.gobbs.pojo.TbbsRegCounter;
import com.litt.core.common.Utility;
import com.litt.core.exception.DBException;

/**
 * 社区统计信息,目前统计信息只保存一条
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2006-09-18
 * @version 1.0
 */
public class RegCounterDAOImpl extends BaseDAOImpl implements IRegCounterDAO
{
	private static final Logger logger = Logger.getLogger(RegCounterDAOImpl.class.getName());
	private static final String BUSI_NAME = "统计信息";
	
	/**
     * 用户点击社区时更新统计相关信息
	 */
	public void updateHits()
	{		
		int today = Utility.getCurrentDateInt();		
		TbbsRegCounter tbbsRegCounter = this.loadLastestCounterInfo();	//获取最新一天的统计信息，如果不是当天则增加
		if(tbbsRegCounter.getCountDay().intValue()<today)	//今天的数据还没有统计
		{
			TbbsRegCounter countToday = new TbbsRegCounter();
			countToday.setCountDay(new Integer(today));
			countToday.setLaterestUser(tbbsRegCounter.getLaterestUser());
			countToday.setMostOnline(tbbsRegCounter.getMostOnline());
			countToday.setTodayHits(0);
			countToday.setTodayReply(0);
			countToday.setTodayTopic(0);
			countToday.setTotalBoy(tbbsRegCounter.getTotalBoy());
			countToday.setTotalGirl(tbbsRegCounter.getTotalGirl());
			countToday.setTotalHits(tbbsRegCounter.getTotalHits());
			countToday.setTotalReply(tbbsRegCounter.getTotalReply());
			countToday.setTotalSecret(tbbsRegCounter.getTotalSecret());
			countToday.setTotalTopic(tbbsRegCounter.getTotalTopic());
			countToday.setYesterHits(tbbsRegCounter.getTodayHits());
			countToday.setYesterReply(tbbsRegCounter.getTodayReply());
			countToday.setYesterTopic(tbbsRegCounter.getTodayTopic());
			super.saveObject(countToday);
		}	
		else
		{			
			tbbsRegCounter.setTodayHits(new Integer(tbbsRegCounter.getTodayHits().intValue()+1));			
			tbbsRegCounter.setTotalHits(new Integer(tbbsRegCounter.getTotalHits().intValue()+1));
			super.updateObject(tbbsRegCounter);
		}		
	}
	
	/**
	 * 用户注册修改用户信息时更新统计相关信息
	 * @param boy	  0 - 没变化，-1 - 男生个数减1，1 - 男人个数加1
	 * @param girl    0 - 没变化，-1 - 女生个数减1，1 - 女人个数加1
	 * @param secret  0 - 没变化，-1 - 保密个数减1，1 - 保密个数加1
	 * @param isNew	  是否是新增用户
	 * @throws DBException
	 */
	public void updateUserModify(int boy,int girl,int secret,boolean isNew,String userName)
	{		
		int today = Utility.getCurrentDateInt();
		
		StringBuilder hql = new StringBuilder("update TbbsRegCounter ");
		hql.append(" set countDay=countDay");	
		if(boy!=0)
		{
			hql.append(" ,totalBoy=totalBoy+"+boy);
		}	
		if(girl!=0)
		{
			hql.append(" ,totalGirl=totalGirl+"+girl);
		}
		if(secret!=0)
		{
			hql.append(" ,totalSecret=totalSecret+"+secret);
		}
		
		if(isNew)
		{
			hql.append(" ,laterestUser='"+userName+"'");
		}
		hql.append(" where countDay=");
		hql.append(today);
		super.executeHql(hql.toString());

	}
	/**
     * 执行删除主题时更新统计信息
	 */	
	public void updateDelTopic()
	{		
		int today = Utility.getCurrentDateInt();
		StringBuilder hql = new StringBuilder("update TbbsRegCounter set todayTopic=todayTopic-1 ,totalTopic = totalTopic-1");
		hql.append(" where countDay=");
		hql.append(today);
		super.executeHql(hql.toString());		
	}

	/**
     * 执行删除回复时更新统计信息
	 */
	public void updateDelRe()
	{		
		int today = Utility.getCurrentDateInt();
		StringBuilder hql = new StringBuilder("update TbbsRegCounter set todayReply=todayReply-1,totalReply = totalReply-1");
		hql.append(" where countDay=");
		hql.append(today);
		super.executeHql(hql.toString());		
	}	
	
	/**
     * 用户增加主题时更新统计相关信息
	 */
	public void updateTodayTopic()
	{		
		int today = Utility.getCurrentDateInt();
		StringBuilder hql = new StringBuilder("update TbbsRegCounter set todayTopic=todayTopic+1 ,totalTopic = totalTopic+1");
		hql.append(" where countDay=");
		hql.append(today);
		super.executeHql(hql.toString());		
	}

	/**
     * 用户增加回复时更新统计相关信息
	 */
	public void updateTodayRe()
	{		
		int today = Utility.getCurrentDateInt();
		StringBuilder hql = new StringBuilder("update TbbsRegCounter set todayReply=todayReply+1,totalReply = totalReply+1");
		hql.append(" where countDay=");
		hql.append(today);
		super.executeHql(hql.toString());		
	}
	
	/**
     * 执行删除分类下所有文章时更新主题数和回复数统计信息
	 */
	public void updateDelClass(int topicCount,int reCount)
	{		
		int today = Utility.getCurrentDateInt();
		StringBuilder hql = new StringBuilder("update TbbsRegCounter ");
		hql.append(" set todayTopic=todayTopic-");
		hql.append(topicCount);
		hql.append(" ,totalReply=totalReply-");
		hql.append(reCount);
		hql.append(" where countDay=");
		hql.append(today);
		super.executeHql(hql.toString());		
	}	
	
	/**
     * 更新最大在线人数
	 */
	public void updateMostOnline(int mostOnline)
	{		
        TbbsRegCounter tbbsRegCounter = loadTodayCounterInfo();
        tbbsRegCounter.setMostOnline(mostOnline);        
		super.updateObject(tbbsRegCounter);
	}	
	
	
	/**
	 * 提取统计信息,只有一条
	 */
	public TbbsRegCounter loadLastestCounterInfo() {
		TbbsRegCounter tbbsRegCounter = null;        
		String hql = "from TbbsRegCounter order by counterId desc";
		tbbsRegCounter = (TbbsRegCounter)super.uniqueResultHql(hql.toString());
		
    	return tbbsRegCounter;
	}
    
    /**
     * 获得当天的统计信息，如果当天没统计则增加一条统计信息
     */
    public TbbsRegCounter loadTodayCounterInfo()
    {       
        int today = Utility.getCurrentDateInt();        
        TbbsRegCounter tbbsRegCounter = this.loadLastestCounterInfo();  //获取最新一天的统计信息，如果不是当天则增加
        if(tbbsRegCounter.getCountDay().intValue()<today)   //今天的数据还没有统计
        {
            TbbsRegCounter countToday = new TbbsRegCounter();
            countToday.setCountDay(new Integer(today));
            countToday.setLaterestUser(tbbsRegCounter.getLaterestUser());
            countToday.setMostOnline(tbbsRegCounter.getMostOnline());
            countToday.setTodayHits(0);
            countToday.setTodayReply(0);
            countToday.setTodayTopic(0);
            countToday.setTotalBoy(tbbsRegCounter.getTotalBoy());
            countToday.setTotalGirl(tbbsRegCounter.getTotalGirl());
            countToday.setTotalHits(tbbsRegCounter.getTotalHits());
            countToday.setTotalReply(tbbsRegCounter.getTotalReply());
            countToday.setTotalSecret(tbbsRegCounter.getTotalSecret());
            countToday.setTotalTopic(tbbsRegCounter.getTotalTopic());
            countToday.setYesterHits(tbbsRegCounter.getTodayHits());
            countToday.setYesterReply(tbbsRegCounter.getTodayReply());
            countToday.setYesterTopic(tbbsRegCounter.getTodayTopic());
            super.saveObject(countToday);
            return countToday;
        }   
        else
            return tbbsRegCounter;
    }

}
